package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;

import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Teacher;
import com.wzx.data.entity.TeacherWork;

import com.wzx.data.service.ITeacherWorkService;

import com.wzx.data.vo.SubjectVo;
import com.wzx.data.vo.TeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 教师作业 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "教师作业管理接口")
@RequestMapping("/wzx/teacherWork")
@Transactional
public class TeacherWorkController {

    @Autowired
    private ITeacherWorkService iTeacherWorkService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(TeacherWork teacherWork) {
        iTeacherWorkService.save(teacherWork);
        return ResultUtil.success();
    }

    @ApiOperation("根据Id获取")
    @GetMapping("getById")
    public Result get(@RequestParam String id) {
        TeacherWork teacherWork = iTeacherWorkService.getById(id);
        return ResultUtil.data(teacherWork);
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public Result update(TeacherWork teacherWork) {
        iTeacherWorkService.updateById(teacherWork);
        return ResultUtil.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam String id) {
        iTeacherWorkService.removeById(id);
        return ResultUtil.success();
    }
    @ApiOperation("查询讲师作业列表")
    @GetMapping("/list")
    public Result list(){
        List<TeacherWork> list = iTeacherWorkService.list();
        return ResultUtil.data(list);
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(@ModelAttribute TeacherWork work, @ModelAttribute PageVo pageVo){
        final LambdaQueryWrapper<TeacherWork> lambda = new QueryWrapper<TeacherWork>().lambda();
        if(ObjectUtils.isNotNull(work.getTeacherId())){
            lambda.eq(TeacherWork::getTeacherId,work.getTeacherId());
        }
        lambda.orderByDesc(TeacherWork::getUpdateTime);
        final IPage<TeacherWork> page = iTeacherWorkService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<TeacherWork> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<TeacherWork> lambda, SubjectVo vo) {

        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(TeacherWork::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(TeacherWork::getCreateTime, vo.getEndTime());
        }
        lambda.eq(TeacherWork::getDelFlag,0);
        lambda.orderByDesc(TeacherWork::getCreateTime);
    }
}
